<template>
	<view class="content b-t">
		<view v-if="empty === true" class="empty">
			<image src="/static/images/noting.png" mode="aspectFit"></image>
			<view class="empty-tips">
				空空如也
				<view class="navigator" @click="addAddress('add')">去添加地址></view>
			</view>
		</view>
		<view v-else>
			<text class="warn_note">
				<text class="yticon icon-bangzhu1"></text>
				重要提醒：请确保地址的正确性
			</text>
			<!-- 列表 -->
			<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="selectAddress(item)">
				<view class="wrapper">
					<view class="address-box">
						<text v-if="item.isdefault" class="tag">默认账户</text>
						<text v-else class="tag_no" @click.stop="selectDefault(item)">设为默认</text>
						<text class="info">{{ item.username }} {{ item.mobile }}</text>
					</view>
					<view class="u-box">
						<text>{{ item.province_text }} {{ item.city_text }} {{ item.district_text }}</text>
					</view>
					<view class="u-box">
						<text>{{ item.address }}</text>
					</view>
				</view>
				<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
				<text class="yticon icon-iconfontshanchu1" @click.stop="delAddress(item)"></text>
			</view>
		</view>

		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			token: '',
			source: 0,
			empty: false, //空白页现实  true|false
			addressList: []
		};
	},
	onLoad(option) {
		this.token = this.$api.get('token');
		if (!this.token) {
			uni.navigateTo('/pages/public/login');
		}

		console.log('option', option);
		if (option.source && option.source != undefined) {
			this.source = option.source;
		}
	},
	onShow() {
		this.loadData();
	},
	watch: {
		//显示空白页
		addressList(e) {
			let empty = e.length === 0 ? true : false;
			if (this.empty !== empty) {
				this.empty = empty;
			}
		}
	},
	methods: {
		//选择地址
		selectAddress(item) {
			console.log(item);
			if (this.source == 1) {
				//this.$api.prePage()获取上一页实例，在App.vue定义
				this.$api.prePage().orderData.address = item;
				this.$api.prePage().addressid = item.id;
				uni.navigateBack();
			}
		},
		addAddress(type, item) {
			uni.navigateTo({
				url: '/pages/address/addressManage?type=' + type + '&data=' + JSON.stringify(item)
			});
		},
		//删除
		delAddress(item) {
			const param = {};
			param.token = this.token;
			param.id = item.id;

			uni.showModal({
				content: '删除收货地址？',
				success: e => {
					if (e.confirm) {
						console.log('User/addressdel', param);
						this.$http.request('User', 'addressdel', 'GET', param, res => {
							console.log('User/addressdel', res);
							if (res.status == 1) {
								if (res.data.error == 0) {
									this.$api.delArrayObj(this.addressList, item);
								}
							}
						});
					}
				}
			});
		},
		//选择默认
		selectDefault(item) {
			const param = {};
			param.token = this.token;
			param.id = item.id;
			param.isdefault = 1;
			
			uni.showModal({
				content: '设为默认收货地址？',
				success: e => {
					if (e.confirm) {
						console.log('User/addressdefault', param);
						this.$http.request('User', 'addressdefault', 'GET', param, res => {
							console.log('User/addressdefault', res);
							if (res.status == 1) {
								if (res.data.error == 0) {
									this.addressList.forEach(res => {
										res.isdefault = 0;
									});
									item.isdefault = 1;
									this.$api.removBehandArrayObj(this.addressList, item);
								}
							}
						});
					}
				}
			});
		},
		async loadData() {
			const param = {};
			param.token = this.token;
			console.log('User/addresslist', param);
			await this.$http.request('User', 'addresslist', 'GET', param, res => {
				console.log('User/addresslist', res);
				if (res.status == 1) {
					if (res.data.error == 0) {
						this.addressList = res.data.data;
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
page {
	padding-bottom: 120upx;
}
.content {
	position: relative;
}
.empty {
	position: fixed;
	left: 0;
	top: 0;
	width: 100%;
	height: 100vh;
	padding-bottom: 100upx;
	display: flex;
	justify-content: center;
	flex-direction: column;
	align-items: center;
	background: #fff;
	image {
		width: 450upx;
		height: 450upx;
		margin-bottom: 30upx;
	}
	.empty-tips {
		display: flex;
		font-size: $font-sm + 2upx;
		color: $font-color-disabled;
		.navigator {
			color: $uni-color-primary;
			margin-left: 16upx;
		}
	}
}
.warn_note {
	display: block;
	padding: 15upx 30upx 15upx 30upx;
	color: #fa436a;
	font-size: 30upx;
	background-color: #EBEEF5;
}
.list {
	display: flex;
	align-items: center;
	padding: 20upx 30upx;
	background: #fff;
	position: relative;
}
.wrapper {
	display: flex;
	flex-direction: column;
	flex: 1;
}
.address-box {
	display: flex;
	align-items: center;
	.tag {
		font-size: 24upx;
		color: $base-color;
		margin-right: 10upx;
		background: #fffafb;
		border: 1px solid #ffb4c7;
		border-radius: 4upx;
		padding: 4upx 10upx;
		line-height: 1;
	}
	.tag_no {
		font-size: 24upx;
		color: #303133;
		margin-right: 10upx;
		background: #e4e7ed;
		border: 1px solid #dcdfe6;
		border-radius: 4upx;
		padding: 4upx 10upx;
		line-height: 1;
	}
	.info {
		font-size: 30upx;
		color: $font-color-dark;
	}
}
.u-box {
	font-size: 28upx;
	color: $font-color-light;
	margin-top: 16upx;
	margin-left: 130upx;
}
.icon-bianji {
	display: flex;
	align-items: center;
	height: 80upx;
	font-size: 40upx;
	color: $font-color-light;
	padding-left: 30upx;
}
.icon-iconfontshanchu1 {
	display: flex;
	align-items: center;
	height: 80upx;
	font-size: 40upx;
	color: $font-color-light;
	padding-left: 20upx;
}

.add-btn {
	position: fixed;
	left: 30upx;
	right: 30upx;
	bottom: 16upx;
	z-index: 95;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 690upx;
	height: 80upx;
	font-size: 32upx;
	color: #fff;
	background-color: $base-color;
	border-radius: 10upx;
	box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
}
</style>
